package com.pjmike.netty.protocol.message.encoder;

import com.pjmike.netty.protocol.message.command.Command;
import com.pjmike.netty.protocol.message.packet.response.AuthResponsePacket;
import com.pjmike.netty.protocol.message.packet.response.HeartbeatResponsePacket;
import com.pjmike.netty.utils.DataTransformUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

import java.util.zip.CRC32;

/**
 * 认证编码器
 *
 * @author lujunjie
 * @create 2021-01-09 09:14
 */
public class AuthEncoder extends MessageToByteEncoder<AuthResponsePacket> {

    @Override
    protected void encode(ChannelHandlerContext ctx, AuthResponsePacket msg, ByteBuf out) throws Exception {
        //报文头
        short bt = Command.BT;
        out.writeByte((byte)bt);
        //协议号
        out.writeShort(Command.AUTH_RESPONSE);
        //设备编号
        out.writeBytes(msg.getSbbh().getBytes());
        //数据长度
        out.writeInt(13);
        //应答
        short apply = msg.getApply();
        out.writeByte((byte)apply);
        //时间
        out.writeLong(msg.getSj());
    }
}
